#include "mainWindow.h"
#include <QUrl>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{ ui->setupUi(this);
  restoreUiState();
  ui->webView->load( QUrl( httpSiteString() ) );
}

MainWindow::~MainWindow()
{ emit closing();
  saveUiState();
  delete ui;
}

void MainWindow::showEvent( QShowEvent * )
{ emit showing(); }

QString MainWindow::ftpSiteString()
{ return "ftp://" + ui->ftpSite->text() + "/weather.png"; }

QString MainWindow::httpSiteString()
{ return "http://" + ui->ftpSite->text() + "/weather.png"; }

void MainWindow::restoreUiState()
{ QSettings settings;
  ui->zipCode  ->setText ( settings.value("zip"  ).toString() );
  ui->ftpSite  ->setText ( settings.value("site" ).toString() );
  ui->username ->setText ( settings.value("user" ).toString() );
  ui->password ->setText ( settings.value("pass" ).toString() );
  ui->radarSite->setText ( settings.value("radar").toString() );
  ui->xOffset  ->setValue( settings.value("xOffs").toInt()    );
  ui->yOffset  ->setValue( settings.value("yOffs").toInt()    );
  ui->startHour->setValue( settings.value("start").toInt()    );
  ui->  endHour->setValue( settings.value("end"  ).toInt()    );
}

void MainWindow::saveUiState()
{ QSettings settings;
  settings.setValue("zip"  , ui->zipCode   ->text());
  settings.setValue("site" , ui->ftpSite   ->text());
  settings.setValue("user" , ui->username  ->text());
  settings.setValue("pass" , ui->password  ->text());
  settings.setValue("radar", ui->radarSite ->text());
  settings.setValue("xOffs", ui->xOffset  ->value());
  settings.setValue("yOffs", ui->yOffset  ->value());
  settings.setValue("start", ui->startHour->value());
  settings.setValue("end"  , ui->  endHour->value());
}
